package com.nlp.visualization.utils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于验证的工具集合
 */
public class VerifyUtils {


    /**
     * 生成指定为位数的数字验证码
     * @param num
     * @return
     */
    public static String ramdomInt(int num) {
        String charValue = "";
        for (int i = 0; i < num; i++) {
            char c = (char) (randomRange(0, 9) + '0');
            charValue += String.valueOf(c);
        }
        return charValue;
    }

    /**
     * 默认六位数字的验证码
     * @return
     */
    public static String ramdomInt() {
        String charValue = "";
        for (int i = 0; i < 6; i++) {
            char c = (char) (randomRange(0, 9) + '0');
            charValue += String.valueOf(c);
        }
        return charValue;
    }

    /**
     * 工具
     * @param from
     * @param to
     * @return
     */
    public static int randomRange(int from, int to) {
        Random r = new Random();
        return from + r.nextInt(to - from);
    }


    /**
     * 通用判断手机是否合法
     * @param telNum
     * @return
     */
    public static boolean isMobiPhoneNum(String telNum){
        String regex = "^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(telNum);
        return m.matches();
    }

    /**
     * 更严格的判断手机是否合法
     * @param telNum
     * @return
     */
    public static boolean isMobileNum(String telNum){
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(telNum);
        return m.matches();
    }

}
